#!/bin/bash

PATH=$PATH

SSHFSCMD="umount"
LOCALBASEPATH="${HOME}/SSHFS"
LOCALHOST=`hostname -s`

CHKPARA=0

if [ -n "$1" ]; then
    SiteCode="$1"
    shift
else
    CHKPARA=1
fi

if [[ ${LOCALHOST} = ${SiteCode} ]]; then
    echo "Error : Do not need to umount your home directory."
    echo ""
    CHKPARA=1
fi

if [ $CHKPARA -eq 1 ]; then
    echo "Usage: $0 SiteCode"
    echo "Example : $0 SSHServerName"
    echo "SiteCode : SSHServerName1 SSHServerName12 labdata SSHServerName"
    echo ""
    echo "SSHFS Mount Status"
    df -h | grep SSHFS
    exit 1
else
    LOCALHOSTPATH=${SiteCode}

    echo "Remote Host : ${SiteCode}"
    echo "Local Host : ${LOCALHOST}"
    echo "Mount Path : ${LOCALBASEPATH}/${LOCALHOSTPATH}"
    echo ""

    if [ -d ${LOCALBASEPATH}/${LOCALHOSTPATH} ]; then
        echo "Check Path : Mount path ${LOCALBASEPATH}/${LOCALHOSTPATH} was existed."
        echo "Umount Command : ${SSHFSCMD} ${LOCALBASEPATH}/${LOCALHOSTPATH}"
        ${SSHFSCMD} ${LOCALBASEPATH}/${LOCALHOSTPATH}
        rmdir ${LOCALBASEPATH}/${LOCALHOSTPATH}
    else
        echo "Error : Mount path ${LOCALBASEPATH}/${LOCALHOSTPATH} does not existed."
    fi

    echo ""
    echo "SSHFS Mount Status"
    df -h | grep SSHFS | grep ${SiteCode}
fi
